<?php
/**
 * Class reading configuration files, returns object of type qConfiguration.
 * Is able to read .ini files.
 */
class qConfig 
{
    /**
     * Load configuration class.
     */
	public static function Init()
	{
	    require_once 'configuration.class.php';
	}

    /**
     * Loads .ini file.
     * @param string $filename
     * @return qConfiguration containing loaded configuration.
     */
	public static function LoadIni($filename)
	{
        // file does not exist - return
	    if (!file_exists($filename))
	    {
	        return false;
	    }

        // initialize file, returned object and section name
	    $file = file($filename);
        $config = new qConfiguration;
        $section = null;
        
        // iterate through every line of file
		for ($i = 0; $i < count($file); $i++)
		{
            // found new section, otherwise $i is incremented
            if ($file[$i][0] == '[')
		    {
                // create qConfiguration object for given section
                $configuration = new qConfiguration;
                
                // delete comment - but i think this is not required here
                if (mb_strpos($file[$i], ';', 0))
                { 
                    $file[$i] = mb_substr($file[$i], 0, mb_strpos($file[$i], ';'));
                }
                
                // find section name
                $currentsection = mb_substr($file[$i], 1, mb_strpos($file[$i], ']') - 1);

                // same section again? continue
		    	if ($currentsection == $section) 
                {
		    		continue;
                }
                
                $section = $currentsection;
                $i++;

                // iterate through lines, until next section
                while ($i < count($file) && $file[$i][0] != '[') 
		        {
                    // drop comments
                    if (mb_strpos($file[$i], ';', 0))
                    { 
                        $file[$i] = mb_substr($file[$i], 0, mb_strpos($file[$i], ';'));
                    }
                    
                    if ($file[$i][0] == ';')
                    {
                        $i++;
                        continue;
                    }
                    
                    // empty line
                    if (trim($file[$i]) == '')
                    {
                        $i++;
                        continue;
                    }

                    // if there is no '=', then return - there is syntax error in file
                    $pos = mb_strpos($file[$i], '=');
                    if (!$pos)
                    {
                        return false;
                    }

                    // get variable name and it's value
                    $varname = trim(mb_substr($file[$i], 0, $pos));
                    $value = trim(mb_substr($file[$i], $pos + 1));
                    
                    // array.key syntax found
                    if (mb_strpos($varname, '.'))
                    {
                        // get each key in an array
                        $varnames = explode('.', $varname);

                        // key[] syntax found
                        if ($varname[mb_strlen($varname) - 2] == '['
                            and $varname[mb_strlen($varname) - 1] == ']')
                        {
                            $varnames[count($varnames) - 1] = mb_substr($varnames[count($varnames) - 1], 0, mb_strlen($varnames[count($varnames) -1]));
                            $varnames[] = -1;
                        }

                        // insert; if failed, return false
                        if (!$configuration->Insert($varnames, $value)) 
                        {
                            unset($config);
                            return false;
                        }
                    }
                    
                    // key[] syntax found
                    else if ($varname[mb_strlen($varname) - 2] == '['
                            and $varname[mb_strlen($varname) - 1] == ']')
                    {
                        // insert; if failed, return false
                        if (!$configuration->Insert(array(mb_substr($varname, 0, mb_strlen($varname) - 2), -1), $value))
                        {
                            unset($config);
                            return false;
                        }
                    }    
                    
                    // no array syntax, simply insert
                    else
                    {
                        $configuration->Insert(array($varname), $value);
                    }
                    
                    // next line
                    $i++;
		        }
                
                // prievous line, to read all of them - $i has been increased without processing line
                $i--;
                // insert section
                $config->Insert(array($section), $configuration);
		    }
		}	    
        
        // return configuration object
        return $config;
	}
    
    /**
     * Dummy function, so far.
     */
    public static function CreateIni($filename, $config = null)
    {
        return true;
    }
}